package utils;

import java.io.*;
import java.nio.charset.StandardCharsets;

public class ZUtils {
    public static String PATH_LOG = "C:\\Users\\win10\\Downloads\\log.txt";

    public static void saveLog(String text) {
        saveTextFile(text, PATH_LOG);
    }

    /**
     * 文字保存为文件
     * @param text
     * @param path
     */
    public static void saveTextFile(String text, String path) {
        try {
            FileOutputStream outputStream = new FileOutputStream(new File(path));
            outputStream.write(text.getBytes(StandardCharsets.UTF_8));
            outputStream.flush();
            outputStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 异常信息保存到文件
     * @param throwable
     */
    public static void saveExceptionLog(Throwable throwable) {
        try {
            throwable.printStackTrace(new PrintWriter(new FileWriter(ZUtils.PATH_LOG),true));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
